package com.pzh.mall.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.pzh.mall.constant.PayStatus;
import com.pzh.mall.constant.PayType;
import com.pzh.mall.dao.OrderMapper;
import com.pzh.mall.model.Order;
import com.pzh.mall.service.IOrderActionService;
import com.pzh.mall.service.IPayService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class PayServiceImpl implements IPayService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private IOrderActionService orderActionService;

    @Transactional
    @Override
    public String doPay(Integer orderId, String payCode, BigDecimal payAmount,String userId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if(payAmount.compareTo(order.getOrderAmount())!=0){
           return "支付金额不正确";
        }
        String payName =PayType.getDescByCode(payCode);
        if(StringUtils.isEmpty(payName)){
            return "支付方式不存在";
        }
        order.setPayStatus(PayStatus.PAID.getCode());
        order.setPayCode(payCode);
        order.setPayName(PayType.getDescByCode(payCode));
        order.setPayTime(System.currentTimeMillis());
        orderMapper.updateByPrimaryKeySelective(order);
        orderActionService.save(order,"支付成功",userId);
        return "success";
    }
}
